﻿'Bram Debouvere

Option Strict On

Imports Gift.BL

Public Class frmPersons

    Private _personenDetailsForm As frmPersonDetails = Nothing

    Private Sub frmPersonen_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        FillItems()
    End Sub

    Private Sub FillItems()
        flvPersonen.Colums = {"Naam", "Voornaam", "Plaats", "Postcode", "Land", "Email"}
        flvPersonen.Items.Clear()
        Dim list As List(Of Person) = Person.GetPersonsFromDatabase(frmMain.dbh)
        For Each person As Person In list
            Dim flvi As New FilteredListViewItem({person.Naam, person.Voornaam, person.Plaats, person.Postcode, person.Land, person.Email}, person)
            flvPersonen.Items.Add(flvi)
        Next
        flvPersonen.FillListItems()
    End Sub

    Private Sub btnNieuw_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNieuw.Click
        frmPersonEditAdd.Person = New Person("", "", "", "", "", "", "", "", "", "", "", "")
        If (frmPersonEditAdd.ShowDialog = Windows.Forms.DialogResult.OK) Then
            Person.AddPersonToDatabase(frmPersonEditAdd.Person, frmMain.dbh)
        End If
        FillItems()
    End Sub

    Private Sub btnAanpassen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAanpassen.Click
        If (Not flvPersonen.SelectedObject Is Nothing) Then
            frmPersonEditAdd.Person = CType(flvPersonen.SelectedObject, Person)
            If (frmPersonEditAdd.ShowDialog = Windows.Forms.DialogResult.OK) Then
                Person.UpdatePersonInDatabase(frmPersonEditAdd.Person, frmMain.dbh)
                FillItems()
            End If
        Else
            MessageBox.Show("Selecteer een item om te bewerken!", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

    Private Sub btnVerwijderen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVerwijderen.Click
        If (Not flvPersonen.SelectedObject Is Nothing) Then
            If (Not Person.CheckPersonForOrderOrList(CType(flvPersonen.SelectedObject, Person), frmMain.dbh)) Then
                If (MessageBox.Show("Weet u zeker dat u dit item wil verwijderen?" & vbCrLf & "Deze actie kan niet ongedaan gemaakt worden", "Verwijderen", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes) Then
                    Facturation.deleteFacturationForPerson(CType(flvPersonen.SelectedObject, Person), frmMain.dbh)
                    Person.DeletePersonFromDatabase(CType(flvPersonen.SelectedObject, Person), frmMain.dbh)
                    FillItems()
                End If
            Else
                MessageBox.Show("Je kunt geen item verwijderen waarvoor er al een bestelling of een lijst bestaat", "Fout", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        Else
            MessageBox.Show("Selecteer een item om te bewerken!", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub


    Private Sub btnDetails_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDetails.Click
        If (Not flvPersonen.SelectedObject Is Nothing) Then
            'only create a new instance of the form if the forum does not yet exist
            If (_personenDetailsForm Is Nothing OrElse _personenDetailsForm.IsDisposed) Then
                _personenDetailsForm = New frmPersonDetails()
            End If
            _personenDetailsForm.Person = CType(flvPersonen.SelectedObject, Person)
            _personenDetailsForm.MdiParent = frmMain
            _personenDetailsForm.Show()
        Else
            MessageBox.Show("Selecteer een persoon!", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

    Private Sub btnBestellingen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBestellingen.Click
        If (Not flvPersonen.SelectedObject Is Nothing) Then
            Dim person As Person = CType(flvPersonen.SelectedObject, Person)
            'show orders for person
            Dim frm As frmOrders = DirectCast(frmMain.showOrderForm(), frmOrders)
            frm.flvOrders.FilterText = person.Naam & " " & person.Voornaam
        Else
            MessageBox.Show("Selecteer een persoon!", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

    Private Sub btnLijsten_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLijsten.Click
        If (Not flvPersonen.SelectedObject Is Nothing) Then
            Dim person As Person = CType(flvPersonen.SelectedObject, Person)
            'show giftlists for person
            Dim frm As frmGiftLists = DirectCast(frmMain.showGiftListForm(), frmGiftLists)
            frm.flvGeschenkenLijsten.FilterText = person.Naam & " " & person.Voornaam
        Else
            MessageBox.Show("Selecteer een persoon!", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub
End Class